package com.geekjob1120.geekjob1120pay.controller;

import com.geekjob1120.geekjob1120pay.modle.Pay;
import com.geekjob1120.geekjob1120pay.modle.PayState;
import com.geekjob1120.geekjob1120pay.service.PayService;
import com.geekjob1120.geekjob1120pay.util.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping("pay")
public class PayController {

    @PostMapping(value = "payService",produces = {MediaType.APPLICATION_JSON_VALUE},consumes = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public Pay payServiceByJson(@RequestBody Pay pay){
        return pay(pay);
    }

    @PostMapping(value = "payService",produces = {MediaType.APPLICATION_XML_VALUE},consumes = {MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    public Pay payServiceByXml(@RequestBody Pay pay){
        return pay(pay);
    }

    private Pay pay(@RequestBody Pay pay){
        PayService payService = SpringBeanUtil.getBean(pay.getPayway().name());
        if (payService == null){
            log.info("不支持当前支付方式:{}",pay.getPayway().getCode());
            return pay;
        }
        if(payService.payService(pay)){
            return pay;
        }
        else if(PayState.PAYING.equals(pay.getState())){
            return pay;
        }
        return pay;
    }
}

